<template>
  <div>
    <el-row :gutter="12">
      <el-col :span="11">
        <el-form-item label="表单元素" :rules="{required: true}">
          <el-select class="el-input" v-model="formData.el" value-key="id" @change="changeEl">
            <el-option :key="index" v-for="(item, index) in elements" :value="item.originalData"
                       :label="item.label"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="11" :offset="1">
        <el-form-item label="元素属性" :rules="{required: true}">
          <el-select class="el-input" v-model="formData.prop" value-key="value" @change="changeProperty">
            <el-option :key="index" v-for="(item, index) in properties" :value="item" :label="item.label"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="1">
        <el-button type="danger" size="mini" icon="el-icon-delete" circle
                   @click="handleDelete(formData.id)"></el-button>
      </el-col>
    </el-row>
    <Component v-if="valueComponent" :is="valueComponent" v-model="formData.value"></Component>
    <el-divider><i class="el-icon-reading"></i></el-divider>
  </div>
</template>

<script>
import conditionMinix from '../condition-minix'
import { Properties as CommonProperties } from './properties/properties'
import Properties from './properties'

export default {
  name: 'property',
  mixins: [conditionMinix],
  components: { ...Properties },
  props: {
    value: {
      type: Object,
      default() {
        return {}
      },
    },
    elements: Array,
    handleDeleteProperty: Function
  },
  data() {
    return {
      formData: this.value,
      valueComponent: '',
      properties: []
    }
  },
  created() {
    this.changeEl(this.formData.el, true)
    this.changeProperty(this.formData.prop, true)
  },
  methods: {
    // 删除属性
    handleDelete(id) {
      this.$emit('handle-delete-property', id)
    },
    // 切换表单元素
    changeEl(el, mutual) {
      if (!el) {
        return
      }
      const name = this.getCompName()
      if (!mutual) {
        this.$set(this.formData, 'prop', '')
        this.$set(this.formData, 'value', '')
        this.valueComponent = ''
      }
      this.properties = CommonProperties[name] || CommonProperties['input']
    },
    // 切换元素属性
    changeProperty(prop, mutual) {
      if (!prop) {
        return
      }
      this.initialValue(mutual)
      this.valueComponent = this.getValueComponent()
    },
    // 获取Value组件
    getValueComponent() {
      const name = this.getCompName()
      if (this.formData.prop.value === 'value') {
        return Properties[`${name}-value`] ? `${name}-value` : 'common-value'

      } else {
        return `common-${this.formData.prop.value}`
      }
    },
    // 初始化值
    initialValue(mutual) {
      if (!mutual) {
        if (this.isRule()) {
          this.$set(this.formData, 'value', [])
        }else if(this.isValue()){
          this.$set(this.formData, 'value', '')
        }else{
          this.$set(this.formData, 'value', false)
        }
      }
    },
    isRule() {
      return this.formData.prop && this.formData.prop.value === 'rules'
    },
    isValue(){
      return this.formData.prop && this.formData.prop.value === 'value'
    }
  }
}
</script>

<style scoped>
</style>
